package com.hq.stream;

import cn.hutool.core.collection.CollUtil;
import com.hq.stream.testdto.User;
import org.junit.Test;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;


@SuppressWarnings("all")
public class StreamUserApiTest {
    static ArrayList<User> users ;
    //复用条件
    static Predicate<User> eq = (a) -> a.getName().equals("b");
    static Predicate<User> gt = (a) -> a.getId() >1;
    static {
        users = CollUtil.toList(new User(1, "a"),
                new User(1, "a"),
                new User(2, "b"),
                new User(3, "bc"),
                new User(4, "d"),
                new User(2, "e"),
                new User(3, "f"),
                new User(4, "g"));
    }
    /**测试谓词 可以复用条件 */
    @Test
    public void predicateTest(){
        List<User> collect = users.stream()
                .filter(eq.and(gt))
                .collect(Collectors.toList());
        collect.forEach(a-> System.out.println(a));
    }
    /**测试Map 可以复用条件 */
    @Test
    public void mapTest(){
        //Function<T, R>   R apply(T t);  传入T 返回 R 要定义返回类型   返回新的对象
        //List<User> collect = users.stream().map(a -> {
        //    a.setName(a.getName() + "test");
        //    return a;
        //}).collect(Collectors.toList());
        ////特殊的map 这个是引用类 传入 Consumer<T>  void accept(T t);  对引用进行修改
        //List<User> collect2 = users.stream().peek(a ->
        //    a.setName(a.getName() + "test")
        //).collect(Collectors.toList());
        //collect.forEach(a-> System.out.println(a));
        //collect2.forEach(a-> System.out.println(a));

        List<String> strings = Arrays.asList("hello", "word");
        List<Stream<String>> collect = strings.stream().map(a -> Arrays.stream(a.split(""))).collect(Collectors.toList());
        List<String[]> collect1 = strings.stream().map(a -> a.split("")).collect(Collectors.toList());
        collect1.forEach(a -> System.out.println(Arrays.toString(a)));
        //flatMap  用于解决上面 多维数组的问题
        List<String> collect3 = strings.stream().flatMap(a -> Arrays.stream(a.split(""))).collect(Collectors.toList());
        collect3.forEach(a -> System.out.println(a));
    }
    @Test
    public void a(){
        int[] arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
                // 首先要创建一个int类型的Spliterator
                // 可以使用Spliterators工厂类为我们创建
       Spliterator.OfInt spliterator = Spliterators.spliterator(arr, 0, arr.length,
                Spliterator.ORDERED | Spliterator.IMMUTABLE);
        // 使用StreamSupport工厂类创建IntStream的实例对象
        IntStream intStream = StreamSupport.intStream(spliterator, false);
        intStream.map(i -> i * i)
                .filter(i -> i % 2 == 0)
                .forEach(System.out::println);
    }


    public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(keyExtractor.apply(t));
    }
}
